package com.algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: sun.hongliang
 * @create: 2019/12/16 11:51
 */
public class TriangleTest {
    public static void main(String[] args) {
        int[][] temp = new int[][]{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
        System.out.println(uniquePathsWithObstacles(temp));
    }

    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<>();
        if (numRows == 0) {
            return result;
        }
        List<Integer> first = new ArrayList<>(1);
        first.add(1);
        result.add(first);
        for (int row = 2; row <= numRows; row++) {
            List<Integer> rowList = new ArrayList<>(row);
            for (int i = 0; i < row; i++) {
                if (i == 0 || i == row - 1) {
                    rowList.add(1);
                } else {
                    List<Integer> preRow = result.get(row - 1);
                    rowList.add(preRow.get(i - 1) + preRow.get(i));
                }
            }
            result.add(rowList);
        }
        return result;
    }

    public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
        System.out.println(Arrays.toString(obstacleGrid[0]));
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        for (int i = 0; i < m; i++) {
            if (obstacleGrid[i][0] == 1) {
                obstacleGrid[i][0] = 0;
                break;
            } else {
                obstacleGrid[i][0] = 1;
            }
        }
        for (int j = 1; j < n; j++) {
            if (obstacleGrid[0][j] == 1) {
                obstacleGrid[0][j] = 0;
                break;
            } else {
                obstacleGrid[0][j] = 1;
            }
        }

        System.out.println(Arrays.toString(obstacleGrid[0]));

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 1) {
                    obstacleGrid[i][j] = 0;
                } else {
                    obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
                }
            }
        }
        return obstacleGrid[m - 1][n - 1];
    }
}
